package com.facebook.app.server.clustering;

import com.facebook.app.shared.clustering.Clusterable;

public class SimpleSimilarityStrategy implements ClusterSimilarityStrategy {
	
	@Override
	public double computeSimilarity(Clusterable first, Clusterable second) {
		
		double[] firstCharacteristicClusterVector = first.getCharacteristicClusterVector();
		double[] secondCharacteristicClusterVector = second.getCharacteristicClusterVector();
		
		if (firstCharacteristicClusterVector.length != secondCharacteristicClusterVector.length) {
			throw new IllegalArgumentException(
					"The lengths of the characteristicClusterVectors are different: "
							+ firstCharacteristicClusterVector.length
							+ " != "
							+ secondCharacteristicClusterVector.length);
		}
		
		double similarity = 0;
		
		for (int i = 0; i < firstCharacteristicClusterVector.length; i++) {
			if (firstCharacteristicClusterVector[i] > 0 && secondCharacteristicClusterVector[i] > 0) {
				similarity += (firstCharacteristicClusterVector[i] + secondCharacteristicClusterVector[i]) / 2.0;
			}
		}
		
		return similarity;		
	}

}
